package com.itqf.utils;

import org.apache.ibatis.io.Resources;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;

import java.io.IOException;
import java.io.InputStream;

/**
 * projectName: mybatisgp8
 *
 * @author: 赵伟风
 * time: 2021/7/29 10:53
 * description: mybatis的工具类
 *
 *    目标：简化获取sqlsession和映射器的过程
 *
 *
 */
public class MyBatisUtils {

    public static SqlSessionFactory sqlSessionFactory;

    static {

        InputStream resourceAsStream = null;
        try {
            resourceAsStream = Resources.getResourceAsStream("mybatis_config.xml");
        } catch (IOException e) {
            e.printStackTrace();
        }
        //执行一次！
        SqlSessionFactoryBuilder sqlSessionFactoryBuilder = new SqlSessionFactoryBuilder();

        sqlSessionFactory = sqlSessionFactoryBuilder.build(resourceAsStream);

    }

    public static SqlSession openSession(){

       return  openSession(false);
    }

    public static SqlSession openSession(Boolean autoCommit){

        SqlSession sqlSession = sqlSessionFactory.openSession(autoCommit);

        return  sqlSession;
    }


}
